package com.tanhua.dubbo.server.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.enums.IdType;
import com.tanhua.dubbo.server.pojo.UserLog;
import com.tanhua.dubbo.server.service.IdService;
import com.tanhua.dubbo.server.vo.UserLocationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: UserLogApiImpl
 * @Author:王晨晗
 * @Description: 用户日志
 * @Date: 2022/01/13/9:16
 */
@Service
public class UserLogApiImpl implements UserLogApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdService idService;
    @Reference
    private UserLocationApi userLocationApi;

    /**
     * @description 执行修改或删除
     * @param userId
     * @param time
     * @return
     */
    @Override
    public void updateOrInsert(Long userId, Long time) {
        //获取到今天的时间区间
        String today = DateUtil.today();
        Long minDate = DateUtil.parseDateTime(today + " 00:00:00").getTime();       //起始时间;
        Long maxDate = DateUtil.parseDateTime(today + " 23:59:59").getTime();       //结束时间;
        //构建查询条件
        Query query = new Query(Criteria.where("userId").is(userId).andOperator(Criteria.where("created").gt(minDate) , Criteria.where("created").lt(maxDate)));
        //查询用户今日是否存在日志信息;
        UserLog userLog = mongoTemplate.findOne(query, UserLog.class);
        //修改条件
        Update update;
        //判断是否存在,构建修改条件;
        if (ObjectUtil.isEmpty(userLog)){
            //查询用户当前登录位置;
            UserLocationVo userLocationVo = userLocationApi.queryByUserId1(userId);
            //新增;
            update = Update.update("countTime" , time).set("userId" , userId)
                    .set("created" , System.currentTimeMillis())
                    .set("lid" , idService.createId(IdType.LOG))
                    .set("address" , userLocationVo.getAddress());
        }else {
            update = Update.update("countTime" , userLog.getCountTime() + time);
        }
        //执行修改;
        mongoTemplate.upsert(query , update , UserLog.class);
    }

    @Override
    public List<UserLog> queryLogs(Integer page, Integer pagesize, Long uid, String sortProp,String sortOrder) {
        PageRequest pageRequest;
        //判断顺序还是倒序排列
        if ("ascending".equals(sortOrder)) {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.asc(sortProp)));//顺序排序
        } else {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.desc(sortProp)));//倒序排序

        }

        //执行查询 获取结果集合
        Query query = Query.query(Criteria.where("userId").is(uid)).with(pageRequest);
        List<UserLog> logList = mongoTemplate.find(query, UserLog.class);
        return logList;


    }
}
